package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author solang
 * @date 2022-12-29 10:40
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetemealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //将套餐存储
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> list(int page, int pageSize, String name) {
        //定义分页对象
        Page<Setmeal> pageInfo = new Page(page, pageSize);
        Page<SetmealDto> pageDtoInfo = new Page(page, pageSize);
        //定义条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, queryWrapper);

        //对象拷贝除dish之外的值
        BeanUtils.copyProperties(pageInfo, pageDtoInfo, "records");
        //获取pageInfo的dish值
        List<Setmeal> list = pageInfo.getRecords();
        //遍历,获取对应分类id
        List<SetmealDto> dtoList = list.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            //使用对应分类id查到category的对应分类
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            //在把分类名取出来放进dishD   to中
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
        //再将list放进pageDtoInfo中返回
        pageDtoInfo.setRecords(dtoList);
        return R.success(pageDtoInfo);
    }

    /**
     * 删除、批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){//RequestParam  RequestBody  PathVariable
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    public R<List<Setmeal>> listSetmeal(Long categoryId,int status){
        System.out.println(categoryId+'_'+status);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(queryWrapper);
        if(list!=null){
            return R.success(list);
        }
        return R.error("现套餐已停售");
    }


}
//class Solution {
//    public int[] twoSum(int[] nums, int target) {
//        //1.创建一个map将数组值存储
//        /**
//         * map.put("nums[0]",0)
//         * map.put("nums[1]",1)
//         * map.put("nums[2]",2)
//         * ...
//         */
//        Map<Integer, Integer> map = new HashMap<>();
//        //开始遍历数组
//        for(int i = 0; i < nums.length; i++) {
//            //使用和数减当前遍历的数,再用map的containsValue(Key)看是否返回true
//            if(map.containsKey(target - nums[i])) {
//                return new int[] {map.get(target-nums[i]),i};
//            }
//            map.put(nums[i], i);
//        }
//        return new int[] {-1,-1};
//    }}
